package com.edgelight.service.impl;

import com.edgelight.dao.SgCommentMapper;
import com.edgelight.pojo.News;
import com.edgelight.pojo.SgComment;
import com.edgelight.pojo.resp.BaseResp;
import com.edgelight.service.SgCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(SgComment)表服务实现类
 *
 * @author makejava
 * @since 2022-08-26 23:36:59
 */
@Service("sgCommentService")
public class SgCommentServiceImpl implements SgCommentService {
    @Resource
    private SgCommentMapper sgCommentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */

    @Override
    public BaseResp insertComment(String comment) {
        BaseResp baseResp = new BaseResp();
        Long articleid = new Long(5);
        try{
            sgCommentMapper.insertComment(comment);
            System.out.println(comment);
        }
        catch (Exception e){
            List<SgComment> sgCommentList =  sgCommentMapper.findIdAndArticleId(articleid);
            baseResp.setData(sgCommentList);
            baseResp.setCode(999);
        }

        List<SgComment> sgCommentList =  sgCommentMapper.findIdAndArticleId(articleid);
        baseResp.setData(sgCommentList);


        return baseResp;
    }


    @Override
    public SgComment queryById(Long id) {
        return this.sgCommentMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sgComment 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SgComment> queryByPage(SgComment sgComment, PageRequest pageRequest) {
        long total = this.sgCommentMapper.count(sgComment);
        return new PageImpl<>(this.sgCommentMapper.queryAllByLimit(sgComment, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sgComment 实例对象
     * @return 实例对象
     */
    @Override
    public SgComment insert(SgComment sgComment) {
        this.sgCommentMapper.insert(sgComment);
        return sgComment;
    }

    /**
     * 修改数据
     *
     * @param sgComment 实例对象
     * @return 实例对象
     */
    @Override
    public SgComment update(SgComment sgComment) {
        this.sgCommentMapper.update(sgComment);
        return this.queryById(sgComment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sgCommentMapper.deleteById(id) > 0;
    }


    @Override
    public BaseResp findIdAndArticleId(Integer page, Integer size, Long articleId) {
        //查询对应文章的根评论

        PageHelper.startPage(page, size);

        //查询全部课程
        List<SgComment> sgCommentList =  sgCommentMapper.findIdAndArticleId(articleId);

        PageInfo<SgComment> of = PageInfo.of(sgCommentList);
        return new BaseResp(1,"ok",of.getList(),of.getTotal());

    }

    @Override
    public BaseResp findAllRoot(Long articleId) {
        List<SgComment> sgCommentList =  sgCommentMapper.findIdAndArticleId(articleId);
        BaseResp baseResp = new BaseResp();
        baseResp.setData(sgCommentList);
        return baseResp;
    }

    @Override
    public BaseResp findAllRootChildren(Long id) {
        List<SgComment> children = sgCommentMapper.findChildren(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setData(children);
        return baseResp;
    }
}
